﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using qiupeng.Public;
using System.IO;
using System.Data.SqlClient;

public partial class SystemManage_DataRevertHyOpen : System.Web.UI.Page
{
    Db List = new Db();
    public static string sDataName, sSettimes;
    public static string sDataUrl, sLjUr, sLjUser, sLjPas, sBfName, sMapFileName;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Session["username"] == null)
        {
            this.Response.Write("<script language=javascript>alert('登陆超时！');window.parent.location = '../main_d.aspx'</script>");
            return;

        }

        if (!Page.IsPostBack)
        {
            BindAttribute();
            string SQL_GetList = "select * from qp_hr_DataBackList where id='" + List.GetFormatStr(Request.QueryString["id"]) + "'";
            OleDbDataReader NewReader = List.GetList(SQL_GetList);
            if (NewReader.Read())
            {
                sDataName = NewReader["DataName"].ToString();
                sSettimes = NewReader["Settimes"].ToString();
                sDataUrl = NewReader["DataUrl"].ToString();

            }
            NewReader.Close();



            string SQL_GetList2 = "select * from qp_hr_DataLjUrl ";
            OleDbDataReader NewReader2 = List.GetList(SQL_GetList2);
            if (NewReader2.Read())
            {
                sLjUr = NewReader2["LjUr"].ToString();
                sLjUser = NewReader2["LjUser"].ToString();
                sLjPas = NewReader2["LjPas"].ToString();
                sBfName = NewReader2["BfName"].ToString();
            }
            NewReader2.Close();
        }



    }
    public void BindAttribute()
    {
        Button1.Attributes["onclick"] = "javascript:return showwait();";

    }



    protected void Button1_Click(object sender, EventArgs e)
    {
        if (exepro() != true)//执行存储过程
        {
            Response.Write("错误，请重试");
        }

        else
        {
            SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
            SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
            try
            {
                oSQLServer.LoginSecure = false;
                oSQLServer.Connect("" + sLjUr + "", "" + sLjUser + "", "" + sLjPas + "");
                oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
                oRestore.Database = "" + sBfName + "";
                oRestore.Files = sDataUrl;
                oRestore.FileNumber = 1;
                oRestore.ReplaceDatabase = true;
                oRestore.SQLRestore(oSQLServer);

                string sql_insertgly = "insert into qp_hr_DataRevertList  (DataName,DataUrl,Username,Realname,Settimes) values ('" + sDataName + "','" + sDataUrl + "','" + Session["username"] + "','" + Session["realname"] + "','" + System.DateTime.Now.ToString() + "')";
                List.ExeSql(sql_insertgly);

                InsertLog("恢复数据库", "数据库恢复");

                this.Response.Write("<script language=javascript>alert('恢复数据库成功！');window.close();</script>"); ;
            }
            catch(Exception err)
            {
                this.Response.Write("<script language=javascript>alert('恢复数据库失败，请重试！');</script>");
            }

            finally
            {
                oSQLServer.DisConnect();
            }
        }




    }

    public void InsertLog(string Name, string MkName)
    {


        string sql_insert_log = "insert into qp_SystemLog (Name,MkName,Username,Realname,Nowtimes,Ip,Unit,UnitId,QxString) "
                                  + "values ('" + Name + "','" + MkName + "','" + this.Session["username"] + "','" + this.Session["realname"] + "','" + System.DateTime.Now.ToString() + "','" + Page.Request.UserHostAddress + "','" + this.Session["Unit"] + "','" + this.Session["UnitId"] + "','" + this.Session["QxString"] + "')";
        List.ExeSql(sql_insert_log);

    }


    private bool exepro()
    {
        //执行存储过程，判定是否还有用户在使用系统
        SqlConnection conn1 = new SqlConnection("server=" + sLjUr + ";uid=" + sLjUser + ";pwd=" + sLjPas + ";database=master");
        SqlCommand cmd = new SqlCommand("killspid", conn1);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@dbname", "" + sBfName + "");
        try
        {
            conn1.Open();
            cmd.ExecuteNonQuery();
            return true;
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message.ToString());
            return false;
        }
        finally
        {
            conn1.Close();
        }


    }

    //得到指定SQL服务器所有数据库的列表
    public ArrayList GetDataBaseList(string ServerName, string UserName, string Pwd)
    {
        ArrayList list = new ArrayList();
        SQLDMO.Application sqlApp = new SQLDMO.Application();
        SQLDMO.SQLServer oServer = new SQLDMO.SQLServer();
        oServer.Connect(ServerName, UserName, Pwd);
        foreach (SQLDMO.Database db in oServer.Databases)
        {
            if ((db.Name != null) && (db.SystemObject == false))
                list.Add(db.Name);
        }
        return list;
    }

    //得到SQL服务器的列表
    public ArrayList GetSqlServerList()
    {
        ArrayList list = new ArrayList();
        SQLDMO.Application sqlApp = new SQLDMO.Application();
        SQLDMO.NameList ServerName = sqlApp.ListAvailableSQLServers();
        for (int i = 0; i < ServerName.Count; i++)
        {
            list.Add(ServerName.Item(i + 1));
        }
        return list;
    }
}
